import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';

Dio dio = new Dio();

class JieWeiQu extends StatefulWidget {
  int rightanswer; //接收答对题数
  int i; //接收答题时间
  String title;
  JieWeiQu(rightanswer, i, title, {Key key}) : super(key: key) {
    this.rightanswer = rightanswer;
    this.i = i;
    this.title = title;
  }

  @override
  _JieWeiQuState createState() => _JieWeiQuState(rightanswer, i, title);
}

class _JieWeiQuState extends State<JieWeiQu> {
  int rightanswer;
  int i; //秒
  String title;
  int jifen;
  var addjifen; //加上后总积分
  var user_id;
  var xuehao;
  _JieWeiQuState(int rightanswer, int i, String title) {
    this.rightanswer = rightanswer;
    this.i = i;
    this.title = title;
  }

  Future shujutijiao() async {
    if (xuehao.length >= 10) {
      //学生
      if (title == '每日答题') {
        if (rightanswer < 2) {
          jifen = 0;
        } else if (rightanswer >= 2 && rightanswer <= 4) {
          jifen = 1;
        } else {
          jifen = 2;
        }
        Response response = await dio
            .get('http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}');
        addjifen = response.data['point'] + jifen;
        if (response.data['mission']['dayli'] != 'true') {
          await dio.patch(
              'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}',
              data: {
                'point': addjifen,
                "mission": {
                  "dayli": "true",
                }
              });
        }
      } else if (title == '每周答题') {
        jifen = rightanswer;
        Response response = await dio
            .get('http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}');
        addjifen = response.data['point'] + jifen;
        if (response.data['mission']['weekend'] != 'true') {
          await dio.patch(
              'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}',
              data: {
                'point': addjifen,
                "mission": {
                  "weekend": "true",
                }
              });
        }
      }
    } else {
      //老师
      if (title == '每日答题') {
        if (rightanswer < 2) {
          jifen = 0;
        } else if (rightanswer >= 2 && rightanswer <= 4) {
          jifen = 1;
        } else {
          jifen = 2;
        }
        Response response = await dio
            .get('http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}');
        addjifen = response.data['point'] + jifen;
        if (response.data['mission']['dayli'] != 'true') {
          await dio.patch(
              'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}',
              data: {
                'point': addjifen,
                "mission": {
                  "dayli": "true",
                }
              });
        }
      } else if (title == '每周答题') {
        jifen = rightanswer;
        Response response = await dio
            .get('http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}');
        addjifen = response.data['point'] + jifen;
        if (response.data['mission']['weekend'] != 'true') {
          await dio.patch(
              'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}',
              data: {
                'point': addjifen,
                "mission": {
                  "weekend": "true",
                }
              });
        }
      }
    }
  }

  void getName() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    user_id = sharedPreferences.getString('user_id');
    xuehao = sharedPreferences.getString('number');
    shujutijiao();
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    getName();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 420,
      margin: EdgeInsets.fromLTRB(20, 60, 20, 0),
      decoration: BoxDecoration(
          color: Colors.white,
          border:
              Border.all(width: 1, color: Color.fromRGBO(220, 220, 220, 1))),
      child: Stack(
        children: <Widget>[
          Container(
            height: 420,
            color: Colors.red,
            child: Image.asset(
              'images/jieweibg.jpg',
              fit: BoxFit.cover,
            ),
          ),
          Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SizedBox(
                  height: 25,
                ),
                Container(
                  margin: EdgeInsets.fromLTRB(15, 0, 0, 0),
                  child: Text('本次答对题目数', style: TextStyle(fontSize: 18)),
                ),
                Container(
                  margin: EdgeInsets.fromLTRB(65, 5, 0, 40),
                  child: Text('${rightanswer}',
                      style:
                          TextStyle(fontSize: 47, fontWeight: FontWeight.bold)),
                ),
                Row(
                  children: <Widget>[
                    SizedBox(
                      width: 20,
                    ),
                    Text('正确率：${(rightanswer / 5) * 100}%'),
                    SizedBox(
                      width: 35,
                    ),
                    Text('用时：${i}s')
                  ],
                ),
                SizedBox(
                  height: 30,
                ),
                Row(
                  children: <Widget>[
                    SizedBox(
                      width: 20,
                    ),
                    Text('错题数：${5 - rightanswer}'),
                    SizedBox(
                      width: 53,
                    ),
                    Text('积分：+${jifen}')
                  ],
                ),
                SizedBox(
                  height: 50,
                ),
                Row(
                  children: <Widget>[
                    SizedBox(
                      width: 90,
                    ),
                    Container(
                      width: 130,
                      child: RaisedButton(
                        color: Colors.brown,
                        child: Text(
                          '返回',
                          style: TextStyle(color: Colors.white),
                        ),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                  ],
                ),
              ])
        ],
      ),
    );
  }
}
